<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择分类"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ARTIFACT_CATEGORY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="材质" prop="material">
        <el-select
          v-model="queryParams.material"
          placeholder="请选择材质"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ARTIFACT_MATERIAL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="年代" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年代"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>

      <el-row justify="center">
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openEditForm('create')"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 信息卡片列表 -->
  <el-row justify="center">
    <el-space wrap>
      <el-card
        @click="openDetailForm(infoObject.id)"
        v-loading="loading"
        v-for="infoObject in infoList"
        :key="infoObject"
        class="box-card"
        shadow="hover"
        style="width: 234px">
        <template #header>
          <div class="card-number">
            <el-text type="info">#000xxx-{{infoObject.id}}</el-text>
          </div>
          <div class="card-name">
            <el-text tag='b' size="large">{{ infoObject.name }}</el-text>
          </div>
        </template>
        <div class="image_error" >
          <el-image style="width: 194px; height: 194px;" :src="getThumbnail(infoObject.id)" fit="cover">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
        </div>
        <el-row :gutter="0">
          <el-col :span="12">
            <el-text class="mx-1">分类: </el-text>
            <dict-tag :type="DICT_TYPE.ARTIFACT_CATEGORY" :value="infoObject.category" />
          </el-col>
          <el-col :span="12">
            <el-text class="mx-1">材质: </el-text>
            <dict-tag :type="DICT_TYPE.ARTIFACT_MATERIAL" :value="infoObject.material" />
          </el-col>
        </el-row>
        <el-row :gutter="0">
          <el-col :span="12">
            <el-text class="mx-1">长度: </el-text>
            <el-text class="mx-1" type="primary" v-if="infoObject.length">{{infoObject.length}}cm</el-text>
            <el-text class="mx-1" type="primary" v-else>-- cm</el-text>
          </el-col>
          <el-col :span="12">
            <el-text class="mx-1">宽度: </el-text>
            <el-text class="mx-1" type="primary" v-if="infoObject.width">{{infoObject.width}}cm</el-text>
            <el-text class="mx-1" type="primary" v-else>-- cm</el-text>
          </el-col>
        </el-row>
        <el-row :gutter="0">
          <el-col :span="12">
            <el-text class="mx-1">高度: </el-text>
            <el-text class="mx-1" type="primary" v-if="infoObject.height">{{infoObject.height}}cm</el-text>
            <el-text class="mx-1" type="primary" v-else>-- cm</el-text>
          </el-col>
          <el-col :span="12">
            <el-text class="mx-1">重量: </el-text>
            <el-text class="mx-1" type="primary" v-if="infoObject.weight">{{infoObject.weight}}g</el-text>
            <el-text class="mx-1" type="primary" v-else>-- g</el-text>
          </el-col>
        </el-row>
        <div v-if="!infoObject.year">
          <el-text>年代: </el-text>
          <el-text type="primary">不详</el-text>
        </div>
        <div v-else-if="infoObject.year >= 0">
          <el-text>公元: </el-text>
          <el-text type="primary">{{infoObject.year}} 年</el-text>
        </div>
        <div v-else>
          <el-text>公元前: </el-text>
          <el-text type="primary">{{-infoObject.year}} 年</el-text>
        </div>
        <div v-if="infoObject.yearDescription">
          <el-text>年代描述: </el-text>
          <el-text type="primary">{{infoObject.yearDescription}}</el-text>
        </div>
        <template #footer>
          <div class="card-footer">
            <div>
              <el-button
                type="primary"
                round
                style="width: 46%"
                @click.stop="openEditForm('update', infoObject.id)"
                >编辑
              </el-button>
              <el-button
                type="danger"
                round
                style="width: 46%"
                @click.stop="handleDelete(infoObject.id)"
                >删除
              </el-button>
            </div>
          </div>
        </template>
      </el-card>
    </el-space>
  </el-row>

  <!-- 分页 -->
  <el-row justify="center">
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <InfoEditForm ref="editFormRef" @success="getList" />
  <!-- 表单弹窗：详细介绍 -->
  <DetailForm ref="detailFormRef" @success="getList" />
</template>


<script setup lang="ts">
import DetailForm from './DetailForm.vue'
import InfoEditForm from './InfoEditForm.vue'
import download from '@/utils/download'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { InfoApi, InfoVO } from '@/api/artifact/info'
import { Picture as IconPicture } from '@element-plus/icons-vue'

/** 文玩信息 列表 */
defineOptions({ name: 'Info' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const infoList = ref<InfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  category: undefined,
  material: undefined,
  weight: undefined,
  length: undefined,
  width: undefined,
  height: undefined,
  year: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const thumbnailMap = new Map() // 缩略图列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await InfoApi.getInfoPage(queryParams)
    infoList.value = data.list
    total.value = data.total
    for (let obj of infoList.value) {
      const imageList = await InfoApi.getInfoImageListByArtifactId(obj.id);
      if (imageList.length !== 0) {
        thumbnailMap.set(obj.id, imageList[0].imagePath)
      }
      else {
        console.log("no image associated with artifact %s", obj.name)
      }
    }
  } finally {
    loading.value = false
  }
}

/** 加载文玩索引图像 */
const getThumbnail = (id: number) => {
  const path = thumbnailMap.get(id);
  if (!path) {
    return ""
  }
  return path
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref()
const openEditForm = (type: string, id?: number) => {
  editFormRef.value.open(type, id)
  return true
}

/** 详细介绍操作 */
const detailFormRef = ref()
const openDetailForm = (id: number) => {
  detailFormRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await InfoApi.deleteInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await InfoApi.exportInfo(queryParams)
    download.excel(data, '文玩信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>




<style scoped>
.image_error .image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 30px;
}
.card-name {
  text-align: left;
}
.card-number {
  text-align: right;
}
</style>
